package com.vincent.kdp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.vincent.kdp.R;
import com.vincent.kdp.ui.bean.response.InStockGoods;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/4 0004.
 */

public class InStockExpandAdapter  extends BaseExpandableListAdapter {
    private Context mContext;
    private List<InStockGoods> mListGoods = new ArrayList<>();
    private CheckBox mCb;

    public InStockExpandAdapter(Context context) {
        mContext = context;
    }

    public void setList(List<InStockGoods> mListGoods, CheckBox cb) {
        this.mListGoods = mListGoods;
        this.mCb = cb;
    }

    @Override
    public int getGroupCount() {
        return mListGoods.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mListGoods.get(groupPosition).getGoodsList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mListGoods.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mListGoods.get(groupPosition).getGoodsList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final GroupViewHolder holder;
        if (convertView == null) {
            holder = new GroupViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_instock_group, parent, false);
            holder.category = (TextView) convertView.findViewById(R.id.category_name);
            holder.totalNum = (TextView) convertView.findViewById(R.id.totalNum);
            holder.totalPrice = (TextView) convertView.findViewById(R.id.totalPrice);
            holder.allTotalWeight = (TextView) convertView.findViewById(R.id.allTotalWeight);
            holder.groupCheck = (CheckBox) convertView.findViewById(R.id.group_check);


            convertView.setTag(holder);
        } else {
            holder = (GroupViewHolder) convertView.getTag();
        }

        holder.groupCheck.setFocusable(false);

        holder.groupCheck.setChecked(check(mListGoods.get(groupPosition).getGroupChecked()));

        holder.groupCheck.setOnClickListener(new Group_CheckBox_Click(groupPosition));

        if (mListGoods.get(groupPosition).getGoodsList().size() > 0) {
            holder.groupCheck.setVisibility(View.VISIBLE);
        } else {
            holder.groupCheck.setVisibility(View.GONE);
        }


        holder.category.setText(mListGoods.get(groupPosition).getCategory());
        holder.totalNum.setText("总数量(件):" + mListGoods.get(groupPosition).getTotalNum());
        holder.totalPrice.setText("总价格(元):" + formatTosepara0(mListGoods.get(groupPosition).getTotalPrice()));
        holder.allTotalWeight.setText("总克重(克):" + formatTosepara0(mListGoods.get(groupPosition).getAllTotalWeight()));


        return convertView;
    }

    class Group_CheckBox_Click implements View.OnClickListener {
        private int groupPosition;

        Group_CheckBox_Click(int groupPosition) {
            this.groupPosition = groupPosition;
        }

        public void onClick(View v) {
            if ("0".equals(mListGoods.get(groupPosition).getGroupChecked())) {
                mListGoods.get(groupPosition).setGroupChecked("1");
            } else if ("1".equals(mListGoods.get(groupPosition).getGroupChecked())) {
                mListGoods.get(groupPosition).setGroupChecked("0");
            }
            // 將 Children 的 isChecked 全面設成跟 Group 一樣
            int childrenCount = mListGoods.get(groupPosition).getGoodsList().size();
            boolean groupIsChecked = check(mListGoods.get(groupPosition).getGroupChecked());
            for (int i = 0; i < childrenCount; i++)
                mListGoods.get(groupPosition).getGoodsList().get(i).setIsChecked(check(groupIsChecked));
            // 注意，一定要通知 ExpandableListView 資料已經改變，ExpandableListView 會重新產生畫面
            checkGroup(groupPosition);
            notifyDataSetChanged();
        }
    }

    public void checkGroup(int groupPosition) {

        int groupCount = mListGoods.size();
        boolean groupAllIsChecked = true;
        for (int i = 0; i < groupCount; i++) {
            if (!check(mListGoods.get(i).getGroupChecked())&&mListGoods.get(i).getGoodsList().size() > 0)
                groupAllIsChecked = false;
        }
        mCb.setChecked(groupAllIsChecked);
    }

    public boolean check(String s) {
        boolean flag = false;
        if (Integer.parseInt(s) == 0) {
            flag = false;
        } else if (Integer.parseInt(s) == 1) {
            flag = true;
        }
        return flag;
    }

    public String check(boolean s) {
        String flag = "0";
        if (s) {
            flag = "1";
        } else {
            flag = "0";
        }
        return flag;
    }


    public static String formatTosepara0(String data) {
        BigDecimal b = new BigDecimal(data);
        DecimalFormat df = new DecimalFormat("#0.000");
        df.setRoundingMode(RoundingMode.FLOOR);
        return df.format(b);
    }

    class GroupViewHolder {
        TextView category;
        TextView totalNum;
        TextView totalPrice;
        TextView allTotalWeight;
        CheckBox groupCheck;

    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ChildViewHolder holder;
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_instock_child, parent, false);
            holder.mName = (TextView) convertView.findViewById(R.id.name);
            holder.mBarCode = (TextView) convertView.findViewById(R.id.bar_code);
            holder.mChargeUnit = (TextView) convertView.findViewById(R.id.charge_unit);
            holder.childCheck = (CheckBox) convertView.findViewById(R.id.child_check);
            holder.noCheck = (TextView) convertView.findViewById(R.id.no_check);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        InStockGoods.InStock entity = mListGoods.get(groupPosition).getGoodsList().get(childPosition);

        holder.childCheck.setChecked(check(entity.getIsChecked()));

        holder.childCheck.setOnClickListener(new Child_CheckBox_Click(groupPosition, childPosition));

        int status = Integer.parseInt(entity.getStatus());
        switch (status) {
            case 0:
                holder.childCheck.setVisibility(View.GONE);
                holder.noCheck.setVisibility(View.VISIBLE);
                holder.noCheck.setText("未入库");
                break;
            case 1:
                if(entity.getInDepart()!=null){
                    if (Integer.parseInt(entity.getInDepart()) == 1) {
                        holder.childCheck.setVisibility(View.VISIBLE);
                        holder.noCheck.setVisibility(View.GONE);
                    }
                }
                break;
            case 4:
                if(entity.getInDepart()!=null){
                    if (Integer.parseInt(entity.getInDepart()) == 1) {
                        holder.childCheck.setVisibility(View.VISIBLE);
                        holder.noCheck.setVisibility(View.GONE);
                    }
                }

                break;
            case 5:
                holder.childCheck.setVisibility(View.GONE);
                holder.noCheck.setVisibility(View.VISIBLE);
                holder.noCheck.setText("已售出");
                break;
            case 6:
                holder.childCheck.setVisibility(View.GONE);
                holder.noCheck.setVisibility(View.VISIBLE);
                holder.noCheck.setText("在途");
                break;
            case 7:
                holder.childCheck.setVisibility(View.GONE);
                holder.noCheck.setVisibility(View.VISIBLE);
                holder.noCheck.setText("退回等待确认");
                break;
            case 8:
                holder.childCheck.setVisibility(View.GONE);
                holder.noCheck.setVisibility(View.VISIBLE);
                holder.noCheck.setText("已被预定");
                break;
        }
        if(entity.getInDepart()!=null){
            if (Integer.parseInt(entity.getInDepart()) == 0) {
                holder.childCheck.setVisibility(View.GONE);
                holder.noCheck.setVisibility(View.VISIBLE);
                holder.noCheck.setText("非本门店");
            }

        }

        holder.mName.setText(entity.getName());
        // holder.mName.setText(entity.getStatus());

        holder.mBarCode.setText(entity.getBarCode());
        if ("1".equals(entity.getTotalUnit())) {
            holder.mChargeUnit.setText("单/克：" + entity.getTotalWeight() + "g");
        } else {
            holder.mChargeUnit.setText("单/克：¥" + entity.getPrice() + "元");
        }
        return convertView;
    }

    class ChildViewHolder {
        TextView mChargeUnit;
        TextView mName;
        TextView mBarCode;
        CheckBox childCheck;
        TextView noCheck;
    }

    class Child_CheckBox_Click implements View.OnClickListener {
        private int groupPosition;
        private int childPosition;

        Child_CheckBox_Click(int groupPosition, int childPosition) {
            this.groupPosition = groupPosition;
            this.childPosition = childPosition;
        }

        public void onClick(View v) {
            handleClick(childPosition, groupPosition);
        }
    }

    public void handleClick(int childPosition, int groupPosition) {

        if ("0".equals(mListGoods.get(groupPosition).getGoodsList().get(childPosition).getIsChecked())) {
            mListGoods.get(groupPosition).getGoodsList().get(childPosition).setIsChecked("1");
        } else if ("1".equals(mListGoods.get(groupPosition).getGoodsList().get(childPosition).getIsChecked())) {
            mListGoods.get(groupPosition).getGoodsList().get(childPosition).setIsChecked("0");
        }

        // 檢查 Child CheckBox 是否有全部勾選，以控制 Group CheckBox
        int childrenCount = mListGoods.get(groupPosition).getGoodsList().size();
        boolean childrenAllIsChecked = true;
        for (int i = 0; i < childrenCount; i++) {
            if (!check(mListGoods.get(groupPosition).getGoodsList().get(i).getIsChecked()))
                childrenAllIsChecked = false;
        }

        mListGoods.get(groupPosition).setGroupChecked(check(childrenAllIsChecked));
        checkGroup(groupPosition);
        // 注意，一定要通知 ExpandableListView 資料已經改變，ExpandableListView 會重新產生畫面
        notifyDataSetChanged();
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
